package com.example.demo.controller;

import com.example.demo.bean.User;
import com.example.demo.domain.GeneralResult;
import com.example.demo.domain.GeneralResult1;
import com.example.demo.server.LoginService;
import com.example.demo.server.UserService;
import io.swagger.annotations.*;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import org.thymeleaf.util.StringUtils;

/***
 * 配合login 页面的登陆代码
 *https://www.bilibili.com/video/BV1gW411W76m?p=34
 *
 * bootstrop
 *
 * https://www.bilibili.com/video/BV1z7411g7kW?p=3
 *
 * 图书管理系统url
 *
 * https://www.bilibili.com/video/BV1gW411W76m?p=38
 */
@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;
    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);

    @ApiOperation(value = "登陆信息验证")
    @RequestMapping(value = "/loginaction", method = RequestMethod.POST)
    @ResponseBody
//    @ApiIgnore//  在swagger 中忽略的注解
    @ApiParam(hidden = true)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "usertel", value = "手机号码", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "username", value = "用户名称", required = true, paramType = "query", dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    public GeneralResult loginaction(User user) {
        GeneralResult generalResult = new GeneralResult();
        if (null == user || user.getUsername().equals("") || user.getUsertel().equals("")) {
            logger.error("统计异常池数量异常,异常信息如下:e.getStackTrace().toString()"
                    + user.getUsername() + user.getUsertest());
        } else {
            generalResult.setCode(200);
            generalResult.setMsg("成功");
            User user1 = loginService.loginaction(user.getUsername(), user.getUsertel());
            generalResult.setData(user1);
            return generalResult;
        }

        return null;
    }

//    @ApiOperation(value = "登陆信息验证")
//    @RequestMapping(value = "/loginactionbyhtml", method = RequestMethod.POST)
//    @ResponseBody
////    @ApiIgnore//  在swagger 中忽略的注解
//    @ApiParam(hidden = true)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "usertel", value = "手机号码", required = true, paramType = "query", dataType = "String"),
//            @ApiImplicitParam(name = "username", value = "用户名称", required = true, paramType = "query", dataType = "String")
//    })
//    @ApiResponses({
//            @ApiResponse(code = 400, message = "请求参数没填好"),
//            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
//    })


    //    username
//    password
    public boolean loginactionbyhtml(String username, String password) {
        if (StringUtils.isEmpty(username) && StringUtils.isEmpty(password) &&
                username.equals("") || password.equals("")) {
            logger.error("统计异常池数量异常,异常信息如下:e.getStackTrace().toString()" + username);
            logger.error("统计异常池数量异常,异常信息如下:e.getStackTrace().toString()" + password);
            return false;
        } else {
            logger.error("统计e.getStackTrace().toString()" + username);
            logger.error("统计:e.getStackTrace().toString()" + password);
//            boolean result = loginService.loginaction(username, password);
//            if (result) {
//                logger.error("统计e.getStackTrace().toString()" + result);
//                return true;
//            } else {
            return false;
//            }
        }

    }
}
